package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Setting_Password extends MyBaseActivity {
    private Context context;
    private MyTitleView password_title;
    private EditText old_password;
    private EditText new_password;
    private Button password_btn;
    private int xx;
    private Handler handler=new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting__password);
        context = Setting_Password.this;
        findView();
    }

    private void findView() {
        password_title= (MyTitleView) findViewById(R.id.password_title);
        old_password= (EditText) findViewById(R.id.old_password);
        new_password= (EditText) findViewById(R.id.new_password);
        password_btn= (Button) findViewById(R.id.password_btn);
        password_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String old_pass=old_password.getText().toString().trim();
                String new_pass=new_password.getText().toString().trim();
                Updata(old_pass,new_pass);
            }
        });
    }

    private void Updata(String old_password,String new_password){
        Map<String,String> map=new HashMap<>();
        map.put("oldPassword",old_password);
        map.put("newPassword",new_password);
        MyOkHttpUtils.getInstance(context).doPost(Constants.RESET_PASSWORD,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if(message!=null){
                            try {
                                JSONObject json=new JSONObject(message);
                                boolean result=json.getBoolean("result");
                                if(result){
                                    MyMethod.showToast(Setting_Password.this,"修改成功");
                                }else{
                                    MyMethod.toActivity(Setting_Password.this,LoginActivty.class);
                                }
                                finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = password_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(password_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                password_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
    }
}
